package com.sousb;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xukai01
 * @since 2011-01-20
 * 
 *        Adapter class of android.util.Log. Extension: Writing text log in log
 *        file.
 * 
 */
class Log {
	public static final int VERBOSE = 2;
	public static final int DEBUG = 3;
	public static final int INFO = 4;
	public static final int WARN = 5;
	public static final int ERROR = 6;
	public static final int ASSERT = 7;

	public static final String TAG = "Mobads SDK";//Baidu
	// public static final String AD_MANAGER = "AdManager";
	// public static final String AD_LOADER = "AdLoader";

	private static final String LOG_FILE = "_b_sdk.log";
	private static final SimpleDateFormat LOG_TIME_FM = new SimpleDateFormat(
			"MM-dd HH:mm:ss.SSS");

	public static void clear() {
		AdUtil.deleteExt(LOG_FILE);
	}

	static {
		//android.util.Log.w("LOG", "static{}");
		clear();
	}

	private synchronized static void debug(String tag, String msg) {
		if (AdConfig.LOG_TO_TEXT) {
			AdUtil.writeExt(LOG_FILE, String.format("%s:: %s\t%s\n",
					LOG_TIME_FM.format(new Date(System.currentTimeMillis())),
					tag, msg), true);
		}
	}

	private static void debug(String tag, String msg, Throwable tr) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		tr.printStackTrace(pw);
		debug(tag, new StringBuilder(msg).append("\n").append(sw.toString())
				.toString());
		pw.close();

		try {
			sw.close();
		} catch (IOException e) {
			android.util.Log.w("Log.debug", "", e);
		}
	}

	// /
	// / Methods from android.util.Log
	// /
	public static boolean isLoggable(String tag, int level) {
		// return android.util.Log.isLoggable(tag, level);
		return level >= AdConfig.LOG_LEVEL;
	}

	public static boolean isLoggable(int level) {
		return isLoggable(TAG, level);
	}

	public static final int UNLOGGABLE=-1;
	public static int v(String tag, String msg) {
		if (!isLoggable(VERBOSE)) {
			return UNLOGGABLE;
		}
		
		debug(tag, msg);
		return android.util.Log.v(tag, msg);
	}

	public static int v(String tag, String msg, Throwable tr) {
		if (!isLoggable(VERBOSE)) {
			return UNLOGGABLE;
		}
		
		debug(tag, msg, tr);
		return android.util.Log.v(tag, msg, tr);
	}

	public static int d(String msg) {
		return d(TAG, msg);
	}
	
	public static int d(String tag, String msg) {
		if (!isLoggable(DEBUG)) {
			return UNLOGGABLE;
		}
		
		debug(tag, msg);
		return android.util.Log.d(tag, msg);
	}

	public static int d(String tag, String msg, Throwable tr) {
		if (!isLoggable(DEBUG)) {
			return UNLOGGABLE;
		}
		
		debug(tag, msg, tr);
		return android.util.Log.d(tag, msg, tr);
	}

	public static int i(String tag, String msg) {
		if (!isLoggable(INFO)) {
			return UNLOGGABLE;
		}
		
		debug(tag, msg);
		return android.util.Log.i(tag, msg);
	}

	public static int i(String tag, String msg, Throwable tr) {
		if (!isLoggable(INFO)) {
			return UNLOGGABLE;
		}
		
		debug(tag, msg, tr);
		return android.util.Log.i(tag, msg, tr);
	}

	public static int w(String tag, String msg) {		
		return w(String.format("%s:: %s", tag, msg));
	}

	public static int w(String msg) {
		if (!isLoggable(WARN)) {
			return UNLOGGABLE;
		}
		
		debug(TAG, msg);
		return android.util.Log.w(TAG, msg);
	}

	public static int w(String tag, String msg, Throwable tr) {
		return w(String.format("%s:: %s", tag, msg), tr);
	}

	public static int w(String msg, Throwable tr) {
		if (!isLoggable(WARN)) {
			return UNLOGGABLE;
		}
		
		debug(TAG, msg, tr);
		return android.util.Log.w(TAG, msg, tr);
	}

	public static int e(String tag, String msg) {
		return e(String.format("%s:: %s", tag, msg));
	}

	public static int e(String msg) {
		if (!isLoggable(ERROR)) {
			return UNLOGGABLE;
		}
		
		debug(TAG, msg);
		return android.util.Log.e(TAG, msg);
	}

	public static int e(String tag, String msg, Throwable tr) {
		return e(String.format("%s:: %s", tag, msg), tr);
	}
	
	public static int e(Throwable tr) {
		return e("", tr);
	}
	
	public static int w(Throwable tr) {
		return w("", tr);
	}
	
	
	public static int e(String msg, Throwable tr) {
		if (!isLoggable(ERROR)) {
			return UNLOGGABLE;
		}
		
		debug(TAG, msg, tr);
		return android.util.Log.e(TAG, msg, tr);
	}
}
